<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>spring boot mybatis 在mapper xml中使用动态sql | 方塘</title>
    <meta name="description" content="半亩方塘 记录点滴">
    <meta name="generator" content="VuePress 1.4.0">
    <link rel="icon" href="/pond/favicon.ico">
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/pond/assets/css/0.styles.e7d3875b.css" as="style"><link rel="preload" href="/pond/assets/js/app.ae40f477.js" as="script"><link rel="preload" href="/pond/assets/js/3.4dddd738.js" as="script"><link rel="preload" href="/pond/assets/js/1.78b3a486.js" as="script"><link rel="preload" href="/pond/assets/js/16.76e81153.js" as="script"><link rel="prefetch" href="/pond/assets/js/10.60d30c62.js"><link rel="prefetch" href="/pond/assets/js/11.2f15c6a5.js"><link rel="prefetch" href="/pond/assets/js/12.1d51f447.js"><link rel="prefetch" href="/pond/assets/js/13.814b881d.js"><link rel="prefetch" href="/pond/assets/js/14.0cad3209.js"><link rel="prefetch" href="/pond/assets/js/15.1f5f9717.js"><link rel="prefetch" href="/pond/assets/js/17.b69f6fef.js"><link rel="prefetch" href="/pond/assets/js/18.8b657c1a.js"><link rel="prefetch" href="/pond/assets/js/19.7b986df4.js"><link rel="prefetch" href="/pond/assets/js/20.dcaf37b7.js"><link rel="prefetch" href="/pond/assets/js/4.cd37660a.js"><link rel="prefetch" href="/pond/assets/js/5.066fd35c.js"><link rel="prefetch" href="/pond/assets/js/6.34341067.js"><link rel="prefetch" href="/pond/assets/js/7.05fd4dd8.js"><link rel="prefetch" href="/pond/assets/js/8.5a84398b.js"><link rel="prefetch" href="/pond/assets/js/9.af98ea9c.js">
    <link rel="stylesheet" href="/pond/assets/css/0.styles.e7d3875b.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div><div class="theme-container" data-v-319dd33c><div data-v-319dd33c><div id="loader-wrapper" class="loading-wrapper" data-v-4b73742e data-v-319dd33c data-v-319dd33c><div class="loader-main" data-v-4b73742e><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-1e2a0cc0 data-v-319dd33c data-v-319dd33c><h3 class="title" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0>方塘</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><input type="password" value="" data-v-1e2a0cc0> <span data-v-1e2a0cc0>Konck! Knock!</span> <button data-v-1e2a0cc0>OK</button></label> <div class="footer" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><span data-v-1e2a0cc0><i class="iconfont reco-theme" data-v-1e2a0cc0></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-1e2a0cc0>vuePress-theme-reco</a></span> <span data-v-1e2a0cc0><i class="iconfont reco-copyright" data-v-1e2a0cc0></i> <a data-v-1e2a0cc0><span data-v-1e2a0cc0>zenos</span>
            
          <span data-v-1e2a0cc0>2017 - </span>
          2020
        </a></span></div></div> <div class="hide" data-v-319dd33c><header class="navbar" data-v-319dd33c><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/pond/" class="home-link router-link-active"><img src="/pond/logo.png" alt="方塘" class="logo"> <span class="site-name">方塘</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/pond/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pond/categories/Git/" class="nav-link"><i class="iconfont undefined"></i>
  Git
</a></li><li class="dropdown-item"><!----> <a href="/pond/categories/Electron/" class="nav-link"><i class="iconfont undefined"></i>
  Electron
</a></li><li class="dropdown-item"><!----> <a href="/pond/categories/Java/问题解决/" class="nav-link"><i class="iconfont undefined"></i>
  Java/问题解决
</a></li><li class="dropdown-item"><!----> <a href="/pond/categories/Nginx/" class="nav-link"><i class="iconfont undefined"></i>
  Nginx
</a></li><li class="dropdown-item"><!----> <a href="/pond/categories/Vue/问题解决/" class="nav-link"><i class="iconfont undefined"></i>
  Vue/问题解决
</a></li></ul></div></div><div class="nav-item"><a href="/pond/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/pond/life/photo.html" class="nav-link"><i class="iconfont reco-message"></i>
  点滴
</a></div><div class="nav-item"><a href="/pond/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://www.npmjs.com/~reco_luan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-npm"></i>
  NPM
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/recoluan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://www.jianshu.com/u/cd674a19515e" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-jianshu"></i>
  简书
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/zhlantian" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-csdn"></i>
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://www.cnblogs.com/luanhewei/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-bokeyuan"></i>
  博客圆
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://mp.weixin.qq.com/s/mXFqeUTegdvPliXknAAG_A" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-wechat"></i>
  WeChat
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-319dd33c></div> <aside class="sidebar" data-v-319dd33c><div class="personal-info-wrapper" data-v-6c8ffc9c><img src="/pond/avatar.png" alt="author-avatar" class="personal-img" data-v-6c8ffc9c> <h3 class="name" data-v-6c8ffc9c>
    zenos
  </h3> <div class="num" data-v-6c8ffc9c><div data-v-6c8ffc9c><h3 data-v-6c8ffc9c>7</h3> <h6 data-v-6c8ffc9c>文章</h6></div> <div data-v-6c8ffc9c><h3 data-v-6c8ffc9c>4</h3> <h6 data-v-6c8ffc9c>标签</h6></div></div> <hr data-v-6c8ffc9c></div> <nav class="nav-links"><div class="nav-item"><a href="/pond/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pond/categories/Git/" class="nav-link"><i class="iconfont undefined"></i>
  Git
</a></li><li class="dropdown-item"><!----> <a href="/pond/categories/Electron/" class="nav-link"><i class="iconfont undefined"></i>
  Electron
</a></li><li class="dropdown-item"><!----> <a href="/pond/categories/Java/问题解决/" class="nav-link"><i class="iconfont undefined"></i>
  Java/问题解决
</a></li><li class="dropdown-item"><!----> <a href="/pond/categories/Nginx/" class="nav-link"><i class="iconfont undefined"></i>
  Nginx
</a></li><li class="dropdown-item"><!----> <a href="/pond/categories/Vue/问题解决/" class="nav-link"><i class="iconfont undefined"></i>
  Vue/问题解决
</a></li></ul></div></div><div class="nav-item"><a href="/pond/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/pond/life/photo.html" class="nav-link"><i class="iconfont reco-message"></i>
  点滴
</a></div><div class="nav-item"><a href="/pond/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://www.npmjs.com/~reco_luan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-npm"></i>
  NPM
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/recoluan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://www.jianshu.com/u/cd674a19515e" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-jianshu"></i>
  简书
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/zhlantian" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-csdn"></i>
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://www.cnblogs.com/luanhewei/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-bokeyuan"></i>
  博客圆
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://mp.weixin.qq.com/s/mXFqeUTegdvPliXknAAG_A" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-wechat"></i>
  WeChat
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>spring boot mybatis 在mapper xml中使用动态sql</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pond/views/Java/%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3/spring%20boot%20mybatis%20%E5%9C%A8mapper%20xml%E4%B8%AD%E4%BD%BF%E7%94%A8%E5%8A%A8%E6%80%81sql.html#场景" class="sidebar-link">场景</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/pond/views/Java/%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3/spring%20boot%20mybatis%20%E5%9C%A8mapper%20xml%E4%B8%AD%E4%BD%BF%E7%94%A8%E5%8A%A8%E6%80%81sql.html#错误详情" class="sidebar-link">错误详情</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/pond/views/Java/%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3/spring%20boot%20mybatis%20%E5%9C%A8mapper%20xml%E4%B8%AD%E4%BD%BF%E7%94%A8%E5%8A%A8%E6%80%81sql.html#错误原因" class="sidebar-link">错误原因</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pond/views/Java/%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3/spring%20boot%20mybatis%20%E5%9C%A8mapper%20xml%E4%B8%AD%E4%BD%BF%E7%94%A8%E5%8A%A8%E6%80%81sql.html#原始代码" class="sidebar-link">原始代码</a></li><li class="sidebar-sub-header"><a href="/pond/views/Java/%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3/spring%20boot%20mybatis%20%E5%9C%A8mapper%20xml%E4%B8%AD%E4%BD%BF%E7%94%A8%E5%8A%A8%E6%80%81sql.html#mapper-中方法声明" class="sidebar-link">Mapper 中方法声明</a></li><li class="sidebar-sub-header"><a href="/pond/views/Java/%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3/spring%20boot%20mybatis%20%E5%9C%A8mapper%20xml%E4%B8%AD%E4%BD%BF%E7%94%A8%E5%8A%A8%E6%80%81sql.html#xml-中动态构造代码" class="sidebar-link">XML 中动态构造代码</a></li></ul></li><li><a href="/pond/views/Java/%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3/spring%20boot%20mybatis%20%E5%9C%A8mapper%20xml%E4%B8%AD%E4%BD%BF%E7%94%A8%E5%8A%A8%E6%80%81sql.html#解决方法" class="sidebar-link">解决方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pond/views/Java/%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3/spring%20boot%20mybatis%20%E5%9C%A8mapper%20xml%E4%B8%AD%E4%BD%BF%E7%94%A8%E5%8A%A8%E6%80%81sql.html#_1、修改-mapper-中的方法，xml-中保持不变-添加-param-adcode-注解，注解中的名称和-xml-中的保持一致" class="sidebar-link">1、修改 Mapper 中的方法，XML 中保持不变 添加@Param(&quot;adCode&quot;)注解，注解中的名称和 xml 中的保持一致</a></li><li class="sidebar-sub-header"><a href="/pond/views/Java/%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3/spring%20boot%20mybatis%20%E5%9C%A8mapper%20xml%E4%B8%AD%E4%BD%BF%E7%94%A8%E5%8A%A8%E6%80%81sql.html#_2、修改-xml-中的变量名称，mapper-不变，-使用-parameter替换-xml-中的变量adcode" class="sidebar-link">2、修改 XML 中的变量名称，Mapper 不变， 使用_parameter替换 XML 中的变量adCode</a></li></ul></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-1e2a0cc0 data-v-319dd33c><h3 class="title" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0>spring boot mybatis 在mapper xml中使用动态sql</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><input type="password" value="" data-v-1e2a0cc0> <span data-v-1e2a0cc0>Konck! Knock!</span> <button data-v-1e2a0cc0>OK</button></label> <div class="footer" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><span data-v-1e2a0cc0><i class="iconfont reco-theme" data-v-1e2a0cc0></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-1e2a0cc0>vuePress-theme-reco</a></span> <span data-v-1e2a0cc0><i class="iconfont reco-copyright" data-v-1e2a0cc0></i> <a data-v-1e2a0cc0><span data-v-1e2a0cc0>zenos</span>
            
          <span data-v-1e2a0cc0>2017 - </span>
          2020
        </a></span></div></div> <div data-v-319dd33c><main class="page"><div class="page-title" style="display:none;"><h1>spring boot mybatis 在mapper xml中使用动态sql</h1> <hr> <div data-v-484a899e><i class="iconfont reco-account" data-v-484a899e><span data-v-484a899e>zenos</span></i> <i class="iconfont reco-date" data-v-484a899e><span data-v-484a899e>2020-05-27</span></i> <!----> <i class="iconfont reco-tag tags" data-v-484a899e><span class="tag-item" data-v-484a899e>
      java
    </span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><div class="custom-block tip"><p class="custom-block-title">错误信息</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>org<span class="token punctuation">.</span>mybatis<span class="token punctuation">.</span>spring<span class="token punctuation">.</span><span class="token class-name">MyBatisSystemException</span><span class="token operator">:</span> nested exception is org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>reflection<span class="token punctuation">.</span><span class="token class-name">ReflectionException</span><span class="token operator">:</span> <span class="token class-name">There</span> is no getter <span class="token keyword">for</span> property named <span class="token string">'adCode'</span> in <span class="token string">'class java.lang.Integer'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></div> <h2 id="场景"><a href="#场景" class="header-anchor">#</a> 场景</h2> <p>spring boot 项目中，在 mybatis mapper 中创建方法，传递参数，并在 xml 中判断参数动态构造 sql 进行查询</p> <h2 id="错误详情"><a href="#错误详情" class="header-anchor">#</a> 错误详情</h2> <div class="language-java line-numbers-mode"><pre class="language-java"><code>org<span class="token punctuation">.</span>mybatis<span class="token punctuation">.</span>spring<span class="token punctuation">.</span><span class="token class-name">MyBatisSystemException</span><span class="token operator">:</span> nested exception is org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>reflection<span class="token punctuation">.</span><span class="token class-name">ReflectionException</span><span class="token operator">:</span> <span class="token class-name">There</span> is no getter <span class="token keyword">for</span> property named <span class="token string">'adCode'</span> in <span class="token string">'class java.lang.Integer'</span>
	at org<span class="token punctuation">.</span>mybatis<span class="token punctuation">.</span>spring<span class="token punctuation">.</span><span class="token class-name">MyBatisExceptionTranslator</span><span class="token punctuation">.</span><span class="token function">translateExceptionIfPossible</span><span class="token punctuation">(</span><span class="token class-name">MyBatisExceptionTranslator</span><span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">77</span><span class="token punctuation">)</span>
	at org<span class="token punctuation">.</span>mybatis<span class="token punctuation">.</span>spring<span class="token punctuation">.</span><span class="token class-name">SqlSessionTemplate</span>$<span class="token class-name">SqlSessionInterceptor</span><span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span><span class="token class-name">SqlSessionTemplate</span><span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">446</span><span class="token punctuation">)</span>
	at com<span class="token punctuation">.</span>sun<span class="token punctuation">.</span>proxy<span class="token punctuation">.</span>$<span class="token class-name">Proxy114</span><span class="token punctuation">.</span><span class="token function">selectOne</span><span class="token punctuation">(</span><span class="token class-name">Unknown</span> <span class="token class-name">Source</span><span class="token punctuation">)</span>
	at org<span class="token punctuation">.</span>mybatis<span class="token punctuation">.</span>spring<span class="token punctuation">.</span><span class="token class-name">SqlSessionTemplate</span><span class="token punctuation">.</span><span class="token function">selectOne</span><span class="token punctuation">(</span><span class="token class-name">SqlSessionTemplate</span><span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">166</span><span class="token punctuation">)</span>
	at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>binding<span class="token punctuation">.</span><span class="token class-name">MapperMethod</span><span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token class-name">MapperMethod</span><span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">82</span><span class="token punctuation">)</span>
	at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>ibatis<span class="token punctuation">.</span>binding<span class="token punctuation">.</span><span class="token class-name">MapperProxy</span><span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span><span class="token class-name">MapperProxy</span><span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">59</span><span class="token punctuation">)</span>
	at com<span class="token punctuation">.</span>sun<span class="token punctuation">.</span>proxy<span class="token punctuation">.</span>$<span class="token class-name">Proxy152</span><span class="token punctuation">.</span><span class="token function">countByRenderStatus</span><span class="token punctuation">(</span><span class="token class-name">Unknown</span> <span class="token class-name">Source</span><span class="token punctuation">)</span>
	at com<span class="token punctuation">.</span>ejudata<span class="token punctuation">.</span>platform<span class="token punctuation">.</span>service<span class="token punctuation">.</span>imp<span class="token punctuation">.</span><span class="token class-name">LayoutMaterialServiceImpl</span><span class="token punctuation">.</span><span class="token function">createRender</span><span class="token punctuation">(</span><span class="token class-name">LayoutMaterialServiceImpl</span><span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">160</span><span class="token punctuation">)</span>
	at com<span class="token punctuation">.</span>ejudata<span class="token punctuation">.</span>platform<span class="token punctuation">.</span>test<span class="token punctuation">.</span><span class="token class-name">TestRenderTask</span><span class="token punctuation">.</span><span class="token function">createRender</span><span class="token punctuation">(</span><span class="token class-name">TestRenderTask</span><span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">25</span><span class="token punctuation">)</span>
	at sun<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span><span class="token class-name">NativeMethodAccessorImpl</span><span class="token punctuation">.</span><span class="token function">invoke0</span><span class="token punctuation">(</span><span class="token class-name">Native</span> <span class="token class-name">Method</span><span class="token punctuation">)</span>
	at sun<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span><span class="token class-name">NativeMethodAccessorImpl</span><span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span><span class="token class-name">NativeMethodAccessorImpl</span><span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">62</span><span class="token punctuation">)</span>
	at sun<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span><span class="token class-name">DelegatingMethodAccessorImpl</span><span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span><span class="token class-name">DelegatingMethodAccessorImpl</span><span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">43</span><span class="token punctuation">)</span>
	at java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span><span class="token class-name">Method</span><span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span><span class="token class-name">Method</span><span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">498</span><span class="token punctuation">)</span>
	at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>model<span class="token punctuation">.</span><span class="token class-name">FrameworkMethod</span>$<span class="token number">1.</span><span class="token function">runReflectiveCall</span><span class="token punctuation">(</span><span class="token class-name">FrameworkMethod</span><span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">50</span><span class="token punctuation">)</span>
	at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>internal<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>model<span class="token punctuation">.</span><span class="token class-name">ReflectiveCallable</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">ReflectiveCallable</span><span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">12</span><span class="token punctuation">)</span>
	at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>model<span class="token punctuation">.</span><span class="token class-name">FrameworkMethod</span><span class="token punctuation">.</span><span class="token function">invokeExplosively</span><span class="token punctuation">(</span><span class="token class-name">FrameworkMethod</span><span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">47</span><span class="token punctuation">)</span>
	at org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>internal<span class="token punctuation">.</span>runners<span class="token punctuation">.</span>statements<span class="token punctuation">.</span><span class="token class-name">InvokeMethod</span><span class="token punctuation">.</span><span class="token function">evaluate</span><span class="token punctuation">(</span><span class="token class-name">InvokeMethod</span><span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">17</span><span class="token punctuation">)</span>
	at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>test<span class="token punctuation">.</span>context<span class="token punctuation">.</span>junit4<span class="token punctuation">.</span>statements<span class="token punctuation">.</span><span class="token class-name">RunBeforeTestMethodCallbacks</span><span class="token punctuation">.</span><span class="token function">evaluate</span><span class="token punctuation">(</span><span class="token class-name">RunBeforeTestMethodCallbacks</span><span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">75</span><span class="token punctuation">)</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h2 id="错误原因"><a href="#错误原因" class="header-anchor">#</a> 错误原因</h2> <p>在 Mapper 方法中定义的参数 与 实体中定义的属性不一致</p> <h3 id="原始代码"><a href="#原始代码" class="header-anchor">#</a> 原始代码</h3> <h3 id="mapper-中方法声明"><a href="#mapper-中方法声明" class="header-anchor">#</a> Mapper 中方法声明</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">SysAd</span><span class="token punctuation">&gt;</span></span> <span class="token function">listByCode</span><span class="token punctuation">(</span><span class="token class-name">String</span> adCode<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="xml-中动态构造代码"><a href="#xml-中动态构造代码" class="header-anchor">#</a> XML 中动态构造代码</h3> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token operator">&lt;</span><span class="token keyword">select</span> id<span class="token operator">=</span><span class="token string">&quot;listByCode&quot;</span> resultMap<span class="token operator">=</span><span class="token string">&quot;BaseResultMap&quot;</span><span class="token operator">&gt;</span>
        <span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> sys_ad <span class="token keyword">where</span>
        <span class="token operator">&lt;</span><span class="token keyword">if</span> test<span class="token operator">=</span><span class="token string">&quot;adCode != null and adCode !=''&quot;</span><span class="token operator">&gt;</span>ad_code <span class="token operator">like</span> CONCAT<span class="token punctuation">(</span><span class="token comment">#{adCode},&quot;%&quot;) and &lt;/if&gt;</span>
        <span class="token keyword">enable</span> <span class="token operator">=</span><span class="token number">1</span> <span class="token keyword">order</span> <span class="token keyword">by</span> ad_code
    <span class="token operator">&lt;</span><span class="token operator">/</span><span class="token keyword">select</span><span class="token operator">&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h2 id="解决方法"><a href="#解决方法" class="header-anchor">#</a> 解决方法</h2> <h3 id="_1、修改-mapper-中的方法，xml-中保持不变-添加-param-adcode-注解，注解中的名称和-xml-中的保持一致"><a href="#_1、修改-mapper-中的方法，xml-中保持不变-添加-param-adcode-注解，注解中的名称和-xml-中的保持一致" class="header-anchor">#</a> 1、修改 Mapper 中的方法，XML 中保持不变 <code>添加@Param(&quot;adCode&quot;)</code>注解，注解中的名称和 xml 中的保持一致</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">SysAd</span><span class="token punctuation">&gt;</span></span> <span class="token function">listByCode</span><span class="token punctuation">(</span><span class="token annotation punctuation">@Param</span><span class="token punctuation">(</span><span class="token string">&quot;adCode&quot;</span><span class="token punctuation">)</span> <span class="token class-name">String</span> adCode<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_2、修改-xml-中的变量名称，mapper-不变，-使用-parameter替换-xml-中的变量adcode"><a href="#_2、修改-xml-中的变量名称，mapper-不变，-使用-parameter替换-xml-中的变量adcode" class="header-anchor">#</a> 2、修改 XML 中的变量名称，Mapper 不变， 使用<code>_parameter</code>替换 XML 中的变量<code>adCode</code></h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token operator">&lt;</span>select id<span class="token operator">=</span><span class="token string">&quot;listByCode&quot;</span> resultMap<span class="token operator">=</span><span class="token string">&quot;BaseResultMap&quot;</span><span class="token operator">&gt;</span>
        select <span class="token operator">*</span> from sys_ad where
        <span class="token operator">&lt;</span><span class="token keyword">if</span> test<span class="token operator">=</span><span class="token string">&quot;_parameter != null and _parameter !=''&quot;</span><span class="token operator">&gt;</span>ad_code like <span class="token function">CONCAT</span><span class="token punctuation">(</span>#<span class="token punctuation">{</span>adCode<span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token string">&quot;%&quot;</span><span class="token punctuation">)</span> and <span class="token operator">&lt;</span><span class="token operator">/</span><span class="token keyword">if</span><span class="token operator">&gt;</span>
        enable <span class="token operator">=</span><span class="token number">1</span> order by ad_code
    <span class="token operator">&lt;</span><span class="token operator">/</span>select<span class="token operator">&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></div> <footer class="page-edit" style="display:none;"><!----> <!----></footer> <!----></main> <!----> <div style="display:none;" data-v-319dd33c data-v-319dd33c><div class="comments-wrapper" data-v-319dd33c><!----></div></div></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-a81d141e data-v-a81d141e><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-a81d141e><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-a81d141e></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-a81d141e></path></svg></div><div class="kanbanniang" data-v-5775ee02><div class="banniang-container" style="display:;" data-v-5775ee02><div class="messageBox" style="right:68px;bottom:190px;display:none;" data-v-5775ee02>
      欢迎来到 方塘
    </div> <div class="operation" style="right:90px;bottom:40px;display:;" data-v-5775ee02><i class="kbnfont kbn-ban-home ban-home" data-v-5775ee02></i> <i class="kbnfont kbn-ban-message message" data-v-5775ee02></i> <i class="kbnfont kbn-ban-close close" data-v-5775ee02></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-5775ee02><i class="kbnfont kbn-ban-info info" data-v-5775ee02></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:none;" data-v-5775ee02></i></div> <canvas id="banniang" width="150" height="220" class="live2d" style="position:fixed;left:0px;bottom:0px;opacity:0.9;z-index:99999;" data-v-5775ee02></canvas></div> <div class="showBanNiang" style="display:none;" data-v-5775ee02>
    看板娘
  </div></div></div></div>
    <script src="/pond/assets/js/app.ae40f477.js" defer></script><script src="/pond/assets/js/3.4dddd738.js" defer></script><script src="/pond/assets/js/1.78b3a486.js" defer></script><script src="/pond/assets/js/16.76e81153.js" defer></script>
  </body>
</html>
